<template>
  <div class="app-container">
    <!-- 添加或修改入伙材料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="材料名称" prop="fileName">
            <el-input v-model="form.fileName" placeholder="请输入材料名称" :disabled="disabled"/>
          </el-form-item>
          <el-form-item label="附件地址" prop="filePath">
            <file-upload
              v-model="form.filePath"
              ref="fileRefs"
              :limit="1"
              @uploadSuccess="fileChange"
            />
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getJoinFile , addJoinFile, updateJoinFile } from "@/api/waitress/joinFile";

  export default {
    name: "JoinFileFrom",
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否禁用
        disabled: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {

    },
    methods: {
      show(option){
        this.reset();
        this.open = true;
        this.title = "新增入伙材料";
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.$emit("cancel")
      },
      // 表单重置
      reset() {
        this.form = {
          fileId: null,
          joinRegisterId: null,
          fileName: null,
          filePath: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$emit("submitForm",this.form)
            this.open = false
            this.reset()
          }
        });
      },
      fileChange(file,fileList){
        this.form.fileName = this.getFileName(file.fileName)
      },
      // 对象转成指定字符串分隔
      getFileName(name) {
        if (!name) return ''
        if (name.lastIndexOf("/") > -1) {
          return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
        } else {
          return "";
        }
      },
    }
  };
</script>
